<template>
  <div class="chat-container">
    <div class="chat-header">
      <h2>{{ title }}</h2>
      <div v-if="chatId" class="chat-id">会话ID: {{ chatId }}</div>
    </div>
    
    <div class="chat-messages" ref="messagesContainer">
      <div v-for="(message, index) in messages" :key="index" 
           :class="['message', getMessageClass(message)]">
        <div class="message-content" :class="{ 'thinking': message.type === 'thinking' }">
          <div v-if="message.type === 'thinking'" class="message-tag">思考过程</div>
          <div v-if="message.type === 'execution'" class="message-tag">执行结果</div>
          <div v-if="message.type === 'error'" class="message-tag error-tag">错误信息</div>
          <div class="message-text">{{ message.content }}</div>
          <div class="message-time">{{ message.time }}</div>
        </div>
      </div>
    </div>
    
    <div class="chat-input">
      <input 
        type="text" 
        v-model="userInput" 
        @keyup.enter="sendMessage" 
        placeholder="请输入消息..." 
        :disabled="loading"
      />
      <button @click="sendMessage" :disabled="loading || !userInput.trim()">
        {{ loading ? '发送中...' : '发送' }}
      </button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ChatInterface',
  props: {
    title: {
      type: String,
      required: true
    },
    chatId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      messages: [],
      userInput: '',
      loading: false,
      currentStreamingMessageIndex: -1
    }
  },
  methods: {
    getMessageClass(message) {
      if (message.type === 'user') return 'user-message';
      if (message.type === 'thinking') return 'thinking-message';
      if (message.type === 'execution') return 'execution-message';
      if (message.type === 'error') return 'error-message';
      return 'ai-message';
    },
    
    sendMessage() {
      if (!this.userInput.trim() || this.loading) return
      
      const userMessage = {
        content: this.userInput,
        type: 'user',
        time: new Date().toLocaleTimeString()
      }
      
      this.messages.push(userMessage)
      this.loading = true
      
      // Emit event to parent component to handle API call
      this.$emit('send-message', this.userInput)
      this.userInput = ''
    },
    
    // Start a new streaming AI message with optional type
    startStreamingMessage(type = 'ai') {
      const aiMessage = {
        content: '',
        type: type,
        time: new Date().toLocaleTimeString()
      }
      this.messages.push(aiMessage)
      this.currentStreamingMessageIndex = this.messages.length - 1
      this.loading = true
      this.scrollToBottom()
    },
    
    // Update the current streaming message with new content
    updateStreamingMessage(content) {
      if (this.currentStreamingMessageIndex >= 0) {
        this.messages[this.currentStreamingMessageIndex].content = content
        this.scrollToBottom()
      }
    },
    
    // Finish the streaming message and reset state
    finishStreamingMessage() {
      this.loading = false
      this.currentStreamingMessageIndex = -1
      this.scrollToBottom()
    },
    
    // Legacy methods kept for compatibility, with optional type parameter
    receiveMessage(content, type = 'ai') {
      const aiMessage = {
        content,
        type,
        time: new Date().toLocaleTimeString()
      }
      this.messages.push(aiMessage)
      this.scrollToBottom()
    },
    
    finishLoading() {
      this.loading = false
      this.scrollToBottom()
    },
    
    scrollToBottom() {
      this.$nextTick(() => {
        if (this.$refs.messagesContainer) {
          this.$refs.messagesContainer.scrollTop = this.$refs.messagesContainer.scrollHeight
        }
      })
    }
  },
  watch: {
    messages: {
      handler() {
        this.scrollToBottom()
      },
      deep: true
    }
  },
  mounted() {
    this.scrollToBottom()
  }
}
</script>

<style scoped>
.chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 800px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.chat-header {
  padding: 15px;
  background-color: #f5f5f5;
  border-bottom: 1px solid #e0e0e0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-header h2 {
  margin: 0;
  font-size: 1.2rem;
}

.chat-id {
  font-size: 0.8rem;
  color: #666;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 15px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background-color: #f9f9f9;
}

.message {
  display: flex;
  margin-bottom: 10px;
}

.user-message {
  justify-content: flex-end;
}

.ai-message, .thinking-message, .execution-message, .error-message {
  justify-content: flex-start;
}

.message-content {
  max-width: 70%;
  padding: 10px 15px;
  border-radius: 18px;
  position: relative;
}

.message-tag {
  font-size: 0.7rem;
  color: #666;
  margin-bottom: 5px;
  font-weight: bold;
}

.error-tag {
  color: #d32f2f;
}

.user-message .message-content {
  background-color: #dcf8c6;
  border-bottom-right-radius: 5px;
}

.ai-message .message-content {
  background-color: #ffffff;
  border-bottom-left-radius: 5px;
}

.thinking-message .message-content {
  background-color: #f0f7ff;
  border-bottom-left-radius: 5px;
  border-left: 3px solid #4285f4;
}

.execution-message .message-content {
  background-color: #ffffff;
  border-bottom-left-radius: 5px;
  border-left: 3px solid #34a853;
}

.error-message .message-content {
  background-color: #ffebee;
  border-bottom-left-radius: 5px;
  border-left: 3px solid #d32f2f;
}

.message-text {
  word-break: break-word;
  white-space: pre-wrap;
}

.message-time {
  font-size: 0.7rem;
  color: #999;
  text-align: right;
  margin-top: 5px;
}

.chat-input {
  display: flex;
  padding: 15px;
  background-color: #f5f5f5;
  border-top: 1px solid #e0e0e0;
}

.chat-input input {
  flex: 1;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 20px;
  outline: none;
  margin-right: 10px;
}

.chat-input button {
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 20px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.chat-input button:hover {
  background-color: #45a049;
}

.chat-input button:disabled {
  background-color: #cccccc;
  cursor: not-allowed;
}
</style> 